/*
 *  Copyright 2019-2025 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package cn.com.cdb.ldms.config.mybatis;

import cn.com.cdb.ldms.handler.CustomOracleIdGenerator;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.incrementer.OracleKeyGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @author jizi
 * @description
 * @date 2025-06-24
 **/
@Configuration
public class MybatisPlusConfig {
    /**
     * MyBatisPlus拦截器（用于分页）
     */
    @Bean
    public MybatisPlusInterceptor paginationInterceptor(
            @Qualifier("databaseIdProvider") DatabaseIdProvider databaseIdProvider
            , @Qualifier("dataSource") DataSource dataSource) {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        try {
            final String databaseId = databaseIdProvider.getDatabaseId(dataSource);
            DbType dbType = null;
            switch (databaseId) {
                case "oracle":
                    dbType = DbType.ORACLE;
                    break;
                case "dm":
                    dbType = DbType.DM;
                    break;
                default:
                    dbType = DbType.MYSQL;
                    break;
            }
            //添加MySQL的分页拦截器
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(dbType));
        } catch (Exception ex) {

        }
        return interceptor;
    }

    // 强制注册生成器
    @Bean
    @Primary
    public OracleKeyGenerator identifierGenerator() {
        return new CustomOracleIdGenerator();
    }

    @Bean
    public DatabaseIdProvider databaseIdProvider() {
        VendorDatabaseIdProvider provider = new VendorDatabaseIdProvider();
        Properties properties = new Properties();
        properties.setProperty("Oracle", "oracle");
        properties.setProperty("DM DBMS", "dm");
        properties.setProperty("MySQL", "mysql");
        provider.setProperties(properties);
        return provider;
    }

}
